package com.gdqy.FCS.web;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.gdqy.FCS.common.api.FastResponse;
import com.gdqy.FCS.common.api.Fileupload;
import com.gdqy.FCS.common.api.ListResponse;
import com.gdqy.FCS.common.mvc.Args;
import com.gdqy.FCS.entity.Clothing;
import com.gdqy.FCS.entity.ClothingType;
import com.gdqy.FCS.entity.Comment;
import com.gdqy.FCS.entity.Design;
import com.gdqy.FCS.entity.DesignerInfo;
import com.gdqy.FCS.entity.DesignerLabel;
import com.gdqy.FCS.entity.Dictionary;
import com.gdqy.FCS.entity.Fabric;
import com.gdqy.FCS.entity.FabricComponent;
import com.gdqy.FCS.entity.Liked;
import com.gdqy.FCS.entity.Special;
import com.gdqy.FCS.entity.User;
import com.gdqy.FCS.service.ClothingService;
import com.gdqy.FCS.service.ClothingTypeService;
import com.gdqy.FCS.service.CollectionService;
import com.gdqy.FCS.service.CommentService;
import com.gdqy.FCS.service.DesignService;
import com.gdqy.FCS.service.DesignerInfoService;
import com.gdqy.FCS.service.DesignerLabelService;
import com.gdqy.FCS.service.DictionaryService;
import com.gdqy.FCS.service.FabricComponentService;
import com.gdqy.FCS.service.FabricService;
import com.gdqy.FCS.service.LikedService;
import com.gdqy.FCS.service.SpecialService;
import com.gdqy.FCS.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("design")
public class DesignController {
	@Autowired
	private DictionaryService dictionaryService;
	@Autowired
	private ClothingTypeService clothingTypeService;
	@Autowired
	private SpecialService specialService;
	@Autowired
	private FabricComponentService fabricComponentService;
	@Autowired
	private UserService userService;
	@Autowired
	private DesignerInfoService designerInfoService;
	@Autowired
	private DesignService designService;
	@Autowired
	private CommentService commentService;
	@Autowired
	private CollectionService collectionService;
	@Autowired
	private LikedService likedService;
	@Autowired
	private DesignerLabelService designerLabelService;
	@Autowired
	private FabricService fabricService;
	@Autowired
	private ClothingService clothingService;

	// 处理增加作品请求，返回modelandview---------测试完毕
	@RequestMapping("deal_addDesign")
	public ModelAndView deal_addDesign(@RequestParam(value = "designer_id", required = true) int designer_id) {
		ModelAndView modelAndView = new ModelAndView();
		// 获取风格
		List<Dictionary> dictionaries = dictionaryService.selectByDicTypeName("风格");

		// 获取大类
		List<ClothingType> clothingTypes = clothingTypeService.selectClothing_class("");
		// 获取款式
		List<Dictionary> pattern = dictionaryService.selectByType_code("ZD005");
		modelAndView.addObject("pattern", pattern);
		// 获取专辑
		List<Special> specials = specialService.selectByDesigner_id(designer_id);
		// 获取面料大类
		List<FabricComponent> fabricComponents = fabricComponentService.selectClass("");

		modelAndView.addObject("style", dictionaries);
		modelAndView.addObject("big_class", clothingTypes);
		modelAndView.addObject("special", specials);
		modelAndView.addObject("fabric_class", fabricComponents);
		modelAndView.addObject("designer_id", designer_id);
		modelAndView.setViewName("designer/addDesign");
		return modelAndView;

	}

	// 增加作品
	@RequestMapping("addDesign")
	public ModelAndView addDesign(Design design, MultipartFile effect_pic, MultipartFile garment_fashion_pic,
			MultipartFile artwork_pic, MultipartFile fabrics_pic_pic, MultipartFile clothes_pic,
			MultipartFile other_pic) throws IllegalStateException, IOException {
		String msg = "";
		ModelAndView modelAndView = new ModelAndView();
		// 判断所传非空
		String designName = design.getName();
		if (designName == "" || designName == null) {
			msg = "上传失败";
			modelAndView.addObject("msg", msg);
			modelAndView.setViewName("designer/addDesign");
			return modelAndView;
		}
		DesignerInfo designerInfo = designerInfoService.selectById(design.getDesigner_id());
		User user = userService.selectById(designerInfo.getUser_id());
		String role = user.getRole();
		String username = user.getUsername();
		String type = "designs";
		Fileupload fileupload = new Fileupload();
		String effect_url = fileupload.upload(effect_pic, role, username, type);
		String garment_fashion_url = fileupload.upload(garment_fashion_pic, role, username, type);
		String artwork_url = fileupload.upload(artwork_pic, role, username, type);
		String fabrics_pic_url = fileupload.upload(fabrics_pic_pic, role, username, type);
		String clothes_pic_url = fileupload.upload(clothes_pic, role, username, type);
		String other_pic_url = fileupload.upload(other_pic, role, username, type);

		// 设置效果图链接
		design.setEffect_url(effect_url);
		// 设置款式图链接
		design.setGarment_fashion_url(garment_fashion_url);
		// 设置工艺图链接
		design.setArtwork_url(artwork_url);
		// 设置面料图链接
		design.setFabrics_pic_url(fabrics_pic_url);
		// 设置成衣图链接
		design.setClothes_pic_url(clothes_pic_url);
		// 设置其他图链接
		design.setOther_pic_url(other_pic_url);

		// 设置被喜欢次数
		design.setLiked(0);
		// 设置被收藏次数
		design.setCollected(0);
		// 设置发布时间
		design.setRelease_time(new Date());
		// 设置修改时间
		design.setUpdate_time(new Date());
		// 设置状态（1=正常、0=删除）
		design.setStatus(1);
		int date = designService.insert(design);
		if (date != 0) {
			msg = "上传成功";
		} else {
			msg = "上传失败";
		}
		modelAndView.addObject("msg", msg);
		modelAndView.setViewName("designer/addDesign");
		return modelAndView;

	}

	// 删除作品
	@RequestMapping("deleteDesign")
	public @ResponseBody Object deleteDesign(Design design, HttpSession session) {
		int design_id = design.getId();
		int designer_id = (int) session.getAttribute("designer_id");
		String msg = "";
		int date = 0;
		// id非空验证
		if (design.getDesigner_id() == designer_id) {
			if (design_id == 0) {
				msg = "删除失败";
			} else {

				// 验证作品是否为该设计师所有

				date = designService.delete(design);
				if (date != 0) {
					msg = "删除成功";
				} else {
					msg = "删除失败";
				}

			}

		}

		return FastResponse._().obj(msg);

	}

	// 处理修改作品页面跳转
	@RequestMapping("deal_modifyDesign")
	public ModelAndView deal_modifyDesign(int design_id) {
		ModelAndView modelAndView = new ModelAndView();
		// 获取作品信息
		Design design = designService.selectById(design_id);
		modelAndView.addObject("design", design);
		// 获取字典风格
		List<Dictionary> dic_style = dictionaryService.selectByDicTypeName("风格");
		modelAndView.addObject("dic_style", dic_style);

		// 获取字典服装大类
		List<ClothingType> clothingTypes = clothingTypeService.selectClothing_class("");
		modelAndView.addObject("big_class", clothingTypes);

		// 获取所有专辑
		List<Special> specials = specialService.selectByDesigner_id(design.getDesigner_id());
		modelAndView.addObject("specials", specials);
		// 获取所在专辑
		Special design_special = specialService.selectById(design.getSpecial_code());
		modelAndView.addObject("design_special", design_special);
		// 获取面料大类
		List<FabricComponent> fabricComponents = fabricComponentService.selectClass("");
		modelAndView.addObject("fabricComponents", fabricComponents);
		// 现在所选的面料小类
		FabricComponent fabric_small_class = fabricComponentService.selectByCode(design.getFabric_code());
		modelAndView.addObject("fabric_small_class", fabric_small_class);
		// 现在所选的面料大类
		FabricComponent fabric_big_class = fabricComponentService.selectByCode(fabric_small_class.getParent_code());
		modelAndView.addObject("fabric_big_class", fabric_big_class);
		// 作品小类
		ClothingType design_small_type = clothingTypeService.selectByCode(design.getType_code());
		modelAndView.addObject("clothing_small_type", design_small_type);
		// 作品中类
		ClothingType design_middle_type = clothingTypeService.selectByCode(design_small_type.getParent_code());
		modelAndView.addObject("clothing_middle_type", design_middle_type);
		// 作品大类
		ClothingType design_big_type = clothingTypeService.selectByCode(design_middle_type.getParent_code());
		modelAndView.addObject("design_big_type", design_big_type);
		// 作品风格
		Dictionary design_style = dictionaryService.selectByCode(design.getStyle_code());
		modelAndView.addObject("design_style", design_style);
		// 款式
		Dictionary design_pattern = dictionaryService.selectByCode(design.getPattern_code());
		modelAndView.addObject("design_pattern", design_pattern);
		// 字典款式
		List<Dictionary> dic_pattern = dictionaryService.selectByType_code("ZD005");
		modelAndView.addObject("dic_pattern", dic_pattern);
		modelAndView.setViewName("designer/modifyDesign");
		return modelAndView;
	}

	// 修改作品
	@RequestMapping("editDesign")
	public @ResponseBody Object editDesign(Design design) {
		String msg = "";
		int design_id = design.getId();
		// id非空验证
		if (design_id == 0) {
			msg = "修改失败";
			return FastResponse._().obj(msg);
		} else {
			// 更新操作
			int date = designService.update(design);
			if (date == 1) {
				msg = "修改成功";
			} else {
				msg = "修改失败";
			}
		}
		return FastResponse._().obj(msg);

	}

	// 显示作品详细
	@RequestMapping("show_design_detail")
	public ModelAndView show_design_detail(@RequestParam(value = "design_id", required = true) int design_id) {
		ModelAndView modelAndView = new ModelAndView();

		// 设计作品
		Design design = designService.selectById(design_id);

		String type_code = design.getType_code();
		String style_code = design.getStyle_code();
		String pattern_code = design.getPattern_code();
		String fabric_code = design.getFabric_code();
		String component_code = design.getFabric_code();

		modelAndView.addObject("design", design);
		DesignerInfo designerInfo = designerInfoService.selectById(design.getDesigner_id());
		modelAndView.addObject("designerInfo", designerInfo);
		// 设计师标签
		List<DesignerLabel> designerLabels = designerLabelService.selectByDesigner_id(designerInfo.getId());
		modelAndView.addObject("designerLabels", designerLabels);
		// 作品分类
		ClothingType design_class = clothingTypeService.selectByCode(type_code);
		modelAndView.addObject("design_class", design_class);
		// 作品风格
		Dictionary design_style = dictionaryService.selectByCode(style_code);
		modelAndView.addObject("design_style", design_style);
		// 作品款式
		Dictionary design_pattern = dictionaryService.selectByCode(pattern_code);
		modelAndView.addObject("design_pattern", design_pattern);
		// 作品面料信息
		FabricComponent fabricComponent = fabricComponentService.selectByCode(fabric_code);
		modelAndView.addObject("fabricComponent", fabricComponent);

		// 相似作品
		List<Design> similarDesign = designService.selectSimilarDesign(type_code, style_code, pattern_code, fabric_code,
				design_id);
		modelAndView.addObject("similarDesign", similarDesign);
		// 匹配面料
		List<Fabric> fabrics = fabricService.selectByComponent_code(component_code);
		modelAndView.addObject("fabrics", fabrics);
		// 匹配服装
		List<Clothing> clothings = clothingService.selectMatchDesignClothing(type_code, style_code, pattern_code,
				component_code);
		modelAndView.addObject("clothings", clothings);

		// 作品评论
		List<Comment> comments = commentService.selectByObj_idAndType(design_id, "1");
		modelAndView.addObject("comments", comments);

		modelAndView.setViewName("designer/designer_design_detail");
		return modelAndView;

	}

	/**
	 * 
	 * 作者：张伟键 方法描述：显示作品详细，因为返回的页面不同，所以这里产生代码的冗余
	 * 
	 * @param design_id
	 * @return
	 */
	@RequestMapping("show_detail")
	public ModelAndView show_detail(@RequestParam(value = "design_id", required = true) int design_id) {
		ModelAndView modelAndView = new ModelAndView();

		// 设计作品
		Design design = designService.selectById(design_id);

		String type_code = design.getType_code();
		String style_code = design.getStyle_code();
		String pattern_code = design.getPattern_code();
		String fabric_code = design.getFabric_code();
		String component_code = design.getFabric_code();
		// 服装大类

		modelAndView.addObject("design", design);
		DesignerInfo designerInfo = designerInfoService.selectById(design.getDesigner_id());
		modelAndView.addObject("designerInfo", designerInfo);
		// 设计师标签
		List<DesignerLabel> designerLabels = designerLabelService.selectByDesigner_id(designerInfo.getId());
		modelAndView.addObject("designerLabels", designerLabels);
		// 作品分类
		ClothingType design_class = clothingTypeService.selectByCode(type_code);
		modelAndView.addObject("design_class", design_class);

		// 作品风格
		Dictionary design_style = dictionaryService.selectByCode(style_code);
		modelAndView.addObject("design_style", design_style);
		// 作品款式
		ClothingType design_pattern = clothingTypeService.selectByCode(pattern_code);
		modelAndView.addObject("design_pattern", design_pattern);
		// 作品面料信息
		FabricComponent fabricComponent = fabricComponentService.selectByCode(fabric_code);
		modelAndView.addObject("fabricComponent", fabricComponent);

		// 相似作品
		List<Design> similarDesign = designService.selectSimilarDesign(type_code, style_code, pattern_code, fabric_code,
				design_id);
		modelAndView.addObject("similarDesign", similarDesign);

		// 匹配面料
		List<Fabric> fabrics = fabricService.selectByComponent_code(component_code);
		modelAndView.addObject("fabrics", fabrics);
		// 匹配服装
		List<Clothing> clothings = clothingService.selectMatchDesignClothing(type_code, style_code, pattern_code,
				component_code);
		modelAndView.addObject("clothings", clothings);

		// 作品评论
		List<Comment> comments = commentService.selectByObj_idAndType(design_id, "1");
		modelAndView.addObject("comments", comments);

		modelAndView.setViewName("designer/detail");
		return modelAndView;
	}

	/*********************** 作品相关 的ajax **************************/
	@RequestMapping("getClothing_class")
	public @ResponseBody List<ClothingType> getClothing_class(String parent_code) {
		List<ClothingType> clothing_class = clothingTypeService.selectClothing_class(parent_code);
		return clothing_class;
	}

	// 获取面料小类
	@RequestMapping("getFabric_small_class")
	public @ResponseBody List<FabricComponent> getFabric_small_class(String parent_code) {
		if (parent_code == null || parent_code == "") {
			return null;
		}
		List<FabricComponent> fabricComponents = fabricComponentService.selectClass(parent_code);
		return fabricComponents;

	}

	

	// 列表浏览设计作品
	@RequestMapping("scan_design")
	public ModelAndView scan_design() {
		ModelAndView modelAndView = new ModelAndView();
		// 女装
		List<Design> designs_lady = designService.selectByBig_code("CL002");
		modelAndView.addObject("desigs_lady", designs_lady);
		// 男装
		List<Design> designs_men = designService.selectByBig_code("CL001");
		modelAndView.addObject("designs_men", designs_men);
		// 童装
		List<Design> designs_child = designService.selectByBig_code("CL003");
		modelAndView.addObject("designs_child", designs_child);
		// 内衣
		List<Design> designs_underwear = designService.selectByBig_code("CL006");
		modelAndView.addObject("designs_underwear", designs_underwear);
		//婚纱
		List<Design> designs_wedding = designService.selectByBig_code("CL004");
		modelAndView.addObject("designs_wedding", designs_wedding);
	
		//作品数量
		int design_count = designService.selectAllCount();
		modelAndView.addObject("design_count", design_count);
		modelAndView.setViewName("designer/design_allList");
		return modelAndView;
	}
	
	//设计师作品
	/**
	 * 
	 *作者：张伟键
	 *方法描述：
	 * @param designer_id 设计师id
	 * @param args 
	 * @param pageindex 第几页
 	 * @param pagesize  页面要多少数据
	 * @param type  请求的类型：设计师个人中心或是浏览设计师
	 * @return
	 */
	@RequestMapping("designer_design")
	public ModelAndView designer_design(@RequestParam(value="designer_id") int designer_id,
			Args args,int pageindex, int pagesize){
		ModelAndView modelAndView = new ModelAndView();
		PageHelper.startPage(pageindex, pagesize);
		List<Design> designs = designService.selectByDesigner_id(designer_id);
		PageInfo<Design> page = new PageInfo<Design>(designs);
		
		//分页对象
		ListResponse<Design> json= FastResponse._().errCode(0).subCode(0).errMsg("获取购买需求成功")
				.args(args.getReqeustargs()).pageIndex(page.getPageNum())
				.pageSize(page.getSize()).total((int)page.getTotal()).pageCount(page.getPages())
				.hasNext(page.isHasNextPage()).list(designs);
		modelAndView.addObject("json", json);
		modelAndView.setViewName("/designer/show_design");
		return modelAndView;
	}
	
	@RequestMapping("scanDesignByBig_code")
	public ModelAndView scanDesignByBig_code(String big_code,Args args,int pageindex, int pagesize) {
		
		ModelAndView modelAndView = new ModelAndView();
		ClothingType dic =clothingTypeService.selectByCode(big_code);
		modelAndView.addObject("dic", dic);
		//分页开始
		PageHelper.startPage(pageindex, pagesize);
		List<Design> designs = designService.selectByBig_code(big_code);
		PageInfo<Design> page = new PageInfo<Design>(designs);
		ListResponse<Design> json= FastResponse._().errCode(0).subCode(0).errMsg("获取购买需求成功")
				.args(args.getReqeustargs()).pageIndex(page.getPageNum())
				.pageSize(page.getSize()).total((int)page.getTotal()).pageCount(page.getPages())
				.hasNext(page.isHasNextPage()).list(designs);
		//分页结束
		modelAndView.addObject("json", json);
		modelAndView.setViewName("designer/design_list");
		return modelAndView;
	}
}
